package controllers

import (
	"gin-AssetManagement/models"
	"gin-AssetManagement/utils"
	"github.com/gin-gonic/gin"
	"github.com/sirupsen/logrus"
	"net/http"
	"strconv"
)

func RolesView(g *gin.Context) {
	g.HTML(http.StatusOK, "role/index.html", nil)
}
func RolesList(g *gin.Context) {
	pageStr := g.Query("page")
	page, _ := strconv.Atoi(pageStr)
	limitStr := g.Query("limit")
	limit, _ := strconv.Atoi(limitStr)
	name := g.Query("name")
	rlesList, count, err := models.GetRolesList(name, page, limit)
	if err != nil {
		g.JSON(http.StatusOK, LayuiData{
			Code:  1,
			Msg:   err.Error(),
			Count: 0,
			Data:  nil,
		})
		return
	}
	g.JSON(http.StatusOK, LayuiData{
		Code:  0,
		Msg:   "成功",
		Count: count,
		Data:  rlesList,
	})
}

func EditRoles(g *gin.Context) {
	id, _ := strconv.Atoi(g.Param("id"))
	useridany, _ := g.Get("userID")
	userid, _ := utils.ConvertToUint(useridany)
	var data RolesFormData
	// 解析前端传来的JSON数据，并将其绑定到menuIDs结构体中
	if err := g.ShouldBindJSON(&data); err != nil {
		g.JSON(http.StatusOK, Respond{
			Code: 1,
			Msg:  "修改角色权限失败，原因是：" + err.Error(),
			Data: nil,
		})
		return
	}
	var roles models.Roles
	roles.Name = data.Name
	roles.Describe = data.Describe
	parseUint, _ := strconv.ParseUint(data.Status, 10, 8)
	roles.IsUsable = uint8(parseUint)
	roles.ModifierID = userid
	err := models.EditRolesMenusById(id, data.MenuIDs, data.AssetsIds, roles)
	if err != nil {
		g.JSON(http.StatusOK, Respond{
			Code: 1,
			Msg:  "修改角色信息失败，原因是：" + err.Error(),
			Data: nil,
		})
		return
	}
	g.JSON(http.StatusOK, Respond{
		Code: 0,
		Msg:  "修改成功",
		Data: nil,
	})
}

func EditROlesView(g *gin.Context) {
	g.HTML(http.StatusOK, "role/edit.html", nil)
}
func EditRolesTreeList(g *gin.Context) {
	id, _ := strconv.Atoi(g.Param("id"))
	var ids []uint
	if id != 1 {
		ids, _ = models.QueryMenuIdById(uint(id))

	}
	menusList, _, err := models.GetMenusList()
	if err != nil {
		g.JSON(http.StatusOK, err.Error())
		return
	}
	roletree := models.RoleTree{}
	rtd := models.RoleTreeData(&roletree, menusList)
	g.JSON(http.StatusOK, gin.H{
		"code": 0,
		"msg":  "成功",
		"data": rtd,
		"ids":  ids,
	})
}

func DelRoles(g *gin.Context) {
	id, _ := strconv.Atoi(g.Param("id"))
	err := models.DelRolesById(id)
	if err != nil {
		g.JSON(http.StatusOK, Respond{
			Code: 1,
			Msg:  "删除失败" + err.Error(),
			Data: nil,
		})
		return
	}
	g.JSON(http.StatusOK, Respond{
		Code: 0,
		Msg:  "删除成功",
		Data: nil,
	})
}
func CreateRolesView(g *gin.Context) {
	g.HTML(http.StatusOK, "role/create.html", nil)
}
func CreateRoles(g *gin.Context) {
	var user models.Users
	useridany, _ := g.Get("userID")
	userid, _ := utils.ConvertToUint(useridany)
	user.ID = userid
	err1 := user.GetUserBasicById()
	if err1 != nil {
		logrus.Error(err1)
		g.JSON(http.StatusOK, Respond{
			Code: 1,
			Msg:  "获取当前用户信息失败",
			Data: nil,
		})
		return
	}
	var data RolesFormData
	// 解析前端传来的JSON数据，并将其绑定到menuIDs结构体中
	if err := g.ShouldBindJSON(&data); err != nil {
		g.JSON(http.StatusOK, Respond{
			Code: 1,
			Msg:  "修改角色权限失败，原因是：" + err.Error(),
			Data: nil,
		})
		return
	}
	var roles models.Roles
	roles.Name = data.Name
	roles.Describe = data.Describe
	parseUint, _ := strconv.ParseUint(data.Status, 10, 8)
	roles.IsUsable = uint8(parseUint)
	roles.Creator = &user
	roles.Modifier = &user
	err := roles.CreateRoles(data.MenuIDs, data.AssetsIds)
	if err != nil {
		g.JSON(http.StatusOK, Respond{
			Code: 1,
			Msg:  "添加失败：" + err.Error(),
			Data: nil,
		})
		return
	}
	g.JSON(http.StatusOK, Respond{
		Code: 0,
		Msg:  "添加成功",
		Data: nil,
	})
}
